import requests as req;
import os;
import time;
import datetime;
from lxml import etree;

def get_list(url,params):
    # UA伪装
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
        "Mobile-User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36"
    }


    html_text = req.get(url=url, params=params, headers=header).text;

    # 加载到对象
    obj = etree.HTML(html_text);
    tables = obj.xpath("//table[@class='tbimg']");

    hrefs = [];
    images = [];
    titles = [];
    for item in tables:
        images = item.xpath("//td[@class='img']//img/@src");
        hrefs = item.xpath("//td[@class='t']/a/@href");
        titles = item.xpath("//td[@class='t']/a/text()");

    print(images)
    print(hrefs)
    print(titles)


if __name__ == '__main__':


    '''
        xpath 解析使用说明
        from lxml import etree;
        
        # 解析本地html资源
        etree_obj = etree.parse(file_path); 
        
        # 解析网络html资源
        etree_obj = etree.HTML(html_text);
        etree_obj.xpath(xpath表达式)
        
        
        xpath只支持逐级标签选择
        xpath可以写多个，使用 | 区分 或的关系 即： item.xpath("//td[@class='img']//img/@src | //td[@class='t']/a/@href");
        
        /:代表一层目录
        //:代表任意位置，其实就是相当于 /html/body/  
        属性定位://div[@class='title']/span
        索引定位://div[@class='title']/span[3]  #获取 <div class='title'> 下的第三个子span，索引从1开始
        取文本:
            /text() #取出子元素文本值
            //text() #取出非子元素文本值
        取属性:
            @src #取出src属性
            
        
    '''

    url = "https://58.com/ershoufang/";

    params = {

    }
    get_list(url,params)

